<?php
/**
 * Created by PhpStorm.
 * User: jocky
 * Date: 14/08/2017
 * Time: 17:34
 */

namespace common\models;

/**
 * 充值相关
 * Class MoneyDao
 * @package common\models
 */
class RechargeDao
{
    /**
     * 充值历史
     *
     * @param int $status
     * @param int $uid
     * @param int $start
     * @param int $end
     * @param int $pageNo
     * @param int $pageSize
     * @param array $orderByArr
     * @return array
     */
    public static function history(
        $status = -1,
        $uid = -1,
        $start = -1,
        $end = -1,
        $pageNo = 1,
        $pageSize = 15,
        $orderByArr = ['id' => 'desc']
    ) {
        $db = \Yii::$app->db;
        $sql_count = "SELECT  COUNT(1) count  FROM `money_order` a ";
        $sql_lists = "SELECT 
                        a.`id`,
                        a.`userID`,
                        a.`user_type`,
                        a.`money_order`,
                        a.`moneys`,
                        a.`time`,
                        a.`value`,
                        b.`loginName`,
                        b.`accountName`
                      FROM  `money_order` a ";

        $sql_where = '';
        $sql_join = "LEFT JOIN `user` b ON b.`userID` = a.`userID`";
        $sql_orderBy = '';
        $sql_limit = " LIMIT " . ($pageNo - 1) * $pageSize . "," . $pageSize;

        $tmpWhere = [];
        $bind_arr = [];

        if ($uid != -1) {
            $tmpWhere[] = 'a.`userID` = :userID';
            $bind_arr[':userID'] = $uid;
        }

        if ($start != -1) {
            $tmpWhere[] = 'a.`time` >= :time_start';
            $bind_arr[':time_start'] = $start;
        }

        if ($end != -1) {
            //结束日期包含当天
            $tmpWhere[] = 'a.`time` < :time_end';
            $bind_arr[':time_end'] = $end;
        }

        if ($status != -1) {
            //充值状态
            $tmpWhere[] = 'a.`user_type` = :status';
            $bind_arr[':status'] = $status;
        }

        if (!empty($tmpWhere)) {
            $sql_where = 'WHERE ' . implode(' AND ', $tmpWhere);
        }
        $tmpOrderByArr = [];
        if (!empty($orderByArr)) {
            $sql_orderBy = " ORDER BY ";
            foreach ($orderByArr as $k => $v) {
                $tmpOrderByArr[] = "a.`" . $k . "` " . $v;
            }
            $sql_orderBy .= implode($tmpOrderByArr, ' , ');
        }
        $tmp = $db->createCommand($sql_count . $sql_join . $sql_where, $bind_arr)->queryOne();
//        var_dump($sql_lists);
//        var_dump($sql_join);
//        var_dump($sql_where);
//        var_dump($sql_orderBy);
//        var_dump($sql_limit);
//        var_dump($bind_arr);
//        exit();
        $lists = $db->createCommand(
            $sql_lists . $sql_join . $sql_where . $sql_orderBy . $sql_limit,
            $bind_arr
        )->queryAll();

//        $order_pay="";
//        if ($uid!=-1){
//            $order_pay="where userID=".$uid."AND a.`time` >= :time_start AND a.`time` < :time_end";
//        }
        $sql_money="select sum(moneys) as moneys from money_order a ";
        $moneys=$db->createCommand($sql_money.$sql_where . $sql_orderBy . $sql_limit,
            $bind_arr)->queryAll();
        return [
            'total' => $tmp['count'] ? $tmp['count'] : 0,
            'lists' => $lists,
            'moneys'=>$moneys
        ];
    }
}
